<?php

class Documents extends CI_Controller {

    function __construct() {
        parent::__construct();
        if ($this->session->userdata('logged_in')) {
            $session_data = $this->session->userdata('logged_in');
            $data['username'] = $session_data['username'];
        } else {
            redirect('user_login/user', 'refresh');
        }
        $this->load->model('document');
        $this->load->model('user');
        $this->load->model('user_role');
        $this->load->library('upload');
        $this->load->library('parser');
        $this->load->library('user_id');
        $this->load->helper('document_helper');
        $this->load->helper('user_helper');

        if ($this->cek() == 0) {
            redirect('user_login/user', 'refresh');
        }
    }

    function index() {
        $data['id'] = $this->user_id->id();

        $data['i'] = 1;
        $data['access'] = $this->user_role->access($this->cek());
        $data['rows'] = $this->document->index();
        $data['content'] = 'documents/index';
        $this->load->view('template/user', $data);
    }

    function add() {
        $data['id'] = $this->user_id->id();

        //cek edit
        $access = $this->user_role->access($this->user_id->cek());
        $this->user_role->chek($access, 'document_add');

        $data['access'] = $access;
        $data['error'] = 0;
        $data['content'] = 'documents/new';
        $this->load->view('template/user', $data);
    }

    function edit() {
        $data['id'] = $this->user_id->id();

        $id = $this->uri->segment(3);
        //cek edit
        $access = $this->user_role->access($this->user_id->cek());
        $this->user_role->chek($access, 'document_edit');

        $data['access'] = $access;
        $data['rows'] = $this->document->edit($id);
        $data['error'] = '';
        $data['content'] = 'documents/edit';
        $this->load->view('template/user', $data);
    }

    function do_upload() {
        // konfigurasi upload berkas
        $config = array(
            'upload_path' => './upload'
            , 'allowed_types' => 'doc|pdf|docx|jpg|png'
            , 'max_size' => '1000000000'
            , 'max_width' => '2048'
            , 'max_height' => '1024'
        );

        $this->upload->initialize($config);

        $pic = '';

        if (isset($_FILES["userfile"]["name"])) {
            if (!$this->upload->do_upload()) {
                //array_push($opr,array('error' => $this->upload->display_errors()));

                $title = $this->input->post('title', TRUE);
                $file = $_FILES["userfile"]["name"];

                $this->document->upload($title, $file);
            } else {
                //array_push($data,array('error' => 'success'));
//                echo"a";

                $tGambar = $this->upload->data();
                $pic = $tGambar["file_name"];
                $title = $this->input->post('title', TRUE);
                $file = $_FILES["userfile"]["name"];
                $this->document->upload($title, $file);
//                echo $file;
                exit;
            }
        } else {
            $pic = '';
        }
    }

    function update() {
        // konfigurasi upload berkas
        $config = array(
            'upload_path' => './upload'
            , 'allowed_types' => 'doc|pdf|docx|jpg|png'
            , 'max_size' => '1000000000'
            , 'max_width' => '2048'
            , 'max_height' => '1024'
        );

        $this->upload->initialize($config);

        $pic = '';

        if (isset($_FILES["userfile"]["name"])) {
            if (!$this->upload->do_upload()) {
                //array_push($opr,array('error' => $this->upload->display_errors()));
                $title = $this->input->post('title', TRUE);
                $file = $_FILES["userfile"]["name"];

                $this->document->update($title, $file);
            } else {
                //array_push($data,array('error' => 'success'));
                $tGambar = $this->upload->data();
                $pic = $tGambar["file_name"];
                $data['error'] = 'File just suport doc, pdf and txt';
                $data['content'] = 'documents/new';
                $this->load->view('template/user', $data);
            }
        } else {
            $pic = '';
        }
    }

    function download() {
        $url = $this->uri->segment(3);
//        $url = 'cokelat_bendera_a';
//        $name = get_download($url);
        $file = get_file($url);
        $data = file_get_contents("upload/" . $file);
        $name = $file;
        force_download($name, $data);
    }

    function delete() {
        //cek edit

        $access = $this->user_role->access($this->user_id->cek());
        $this->user_role->chek($access, 'document_delete');

        $url = $this->uri->segment(3);
        $url = explode('=', $url);
        $id = $url[0];
        $file = $url[1];
        $this->document->delete($id, $file);
//       
        $file = get_file($file);

//        $path_to_file = 'D:/xampplite/htdocs/st/upload/'.$file;
        $path_to_file = '/home/stasi904/public_html/upload/'.$file;
        
        if (unlink($path_to_file)) {
            $delete =  'successfully';
        } else {
            $delete = 'errors';
        }
        redirect('documents/index/');
    }

    function id() {
        $session_data = $this->session->userdata('logged_in');
        return $session_data['id'];
    }

    function cek() {
        $level = $this->user->cek($this->id());
        if ($level) {
            $level;
        } else {
            $level = 0;
        }
        return $level;
    }

}

?>
